package al.jdbc.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import al.jdbc.dpbridge.DataSource;
import alOmrane.jdbc.beans.Contact;
import alOmrane.jdbc.beans.Utilisateur;

public class UtilisateurDAO {
	private Connection db;
	@SuppressWarnings("unused")
	private DataSource ds;
	private Statement sql;
	
	public UtilisateurDAO(DataSource ds){
		db = ds.getConnection();
		this.ds = ds;
		try {
			sql = db.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isExist(String user){
		try {
			String query = "SELECT * FROM utilisateur WHERE username='"+user+"' ;";
			ResultSet rs = sql.executeQuery(query);
			if(rs.next())
				return true;
			else return false;
		}catch(Exception ex){
			return false;
		}
	}
	public Utilisateur findByUsername(String user){
		Utilisateur u ;
		try {
			String query = "SELECT * FROM utilisateur WHERE username='"+user+"' ;";
			ResultSet rs = sql.executeQuery(query);
			rs.next();
			u = new Utilisateur(Integer.parseInt(rs.getString(1)), 
					rs.getString(2),
					rs.getString(3),
					rs.getString(4),
					rs.getString(5));
			return u;
		}catch(Exception ex){
			return null;
		}
	}
	public boolean insert(Utilisateur user){
		return false;
	}
}
